package com.pboc.service;

import com.pboc.base.BaseException;
import com.pboc.base.PageModel;
import com.pboc.dao.SysDeptDao;
import com.pboc.dao.SysPersonDao;
import com.pboc.entity.SysDept;
import com.pboc.entity.SysPerson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
public class SysDeptService {
    @Autowired
    private SysDeptDao sysDeptDao;
    @Autowired
    private SysPersonDao sysPersonDao;
   public List<SysDept> getList(SysDept sysDept){
       return sysDeptDao.getList(sysDept);
   }

    public void getList(PageModel<SysDept> pageModel){
        try {
            pageModel.setRows(sysDeptDao.pageQuery(pageModel));
            pageModel.setTotal(sysDeptDao.getListCount(pageModel.getQueryObj()));
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public SysDept getById(Integer id){
        return sysDeptDao.getById(id);
    }
    public void addDept(SysDept dept){
        sysDeptDao.save(dept);
    }
    public void editDept(SysDept dept){
        sysDeptDao.update(dept);
    }
    public void delDept(Integer id) throws BaseException {
        SysPerson person=new SysPerson();
        person.setDeptId(id);
        List<SysPerson> people=sysPersonDao.getList(person);
        if(CollectionUtils.isEmpty(people)){
            sysDeptDao.delete(id);
        }else {
            throw new BaseException("该部门下存在员工，请先清除部门下员工信息");
        }
    }
}
